//
//  CXChannelInterActionController.m
//  ProjectOfBStage
//
//  Created by cuixin on 15/11/13.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "CXChannelInterActionController.h"
#import "CustomerNetworking.h"
#import "CXFirstTableViewCell.h"
#import "CXInterActionFirstModel.h"
#import "CXFisrtActionColelctionCell.h"
#import "CXSecondCollectionViewCell.h"
#import "CXSecondTableViewCell.h"
#import "CXInterSecondModel.h"
#import "CXThirdModel.h"
#import "CXThirdCollectionViewCell.h"
#import "CXThirdTableViewCell.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface CXChannelInterActionController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *firstArray;
@property(nonatomic, retain)NSMutableArray *secondArray;
@property(nonatomic, retain)NSMutableArray *thirdArray;
//@property(nonatomic, retain)UICollectionView *collectionView;

@property(nonatomic, retain)CXFirstTableViewCell *cell;

@property(nonatomic, retain)CXSecondTableViewCell *secondCell;

@property(nonatomic, retain)CXThirdTableViewCell *thirdCell;
@end

@implementation CXChannelInterActionController
-(void)dealloc{
    [_firstArray release];
    [_secondArray release];
    [_thirdArray release];
    [_tableView release];
//    [_collectionView release];
//    [_cell release];
//    [_secondCell release];
//    [_thirdCell release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.secondArray = [NSMutableArray array];
    self.thirdArray = [NSMutableArray array];
    self.firstArray = [NSMutableArray array];
    
    [self loadThirdData];
    [self loadData];
    [self loadSecondData];

    
    [self createTableView];

}
-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 60) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
#pragma mark 解析相关频道数据
-(void)loadData{
    [CustomerNetworking customerWithURL:@"http://echosystem.kibey.com/echofamous/tv/tvs" andParamer:nil andBlock:^(id response) {
                for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"channels"]) {
                    CXInterActionFirstModel *model = [CXInterActionFirstModel modelWithDictionary:dic];
                    [self.thirdArray addObject:model];
                }
        [self.tableView reloadData];
            }];

}

#pragma mark 解析第二个cell数据
-(void)loadSecondData{
    [CustomerNetworking customerWithURL:@"http://echosystem.kibey.com/echofamous/tv/tvs" andParamer:nil andBlock:^(id response) {
        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"users"]) {
            CXInterSecondModel *model = [CXInterSecondModel modelWithDictionary:dic];
            [self.secondArray addObject:model];
        }
        [self.tableView reloadData];
        
    }];
}

#pragma mark 解析第三种cell数据
-(void)loadThirdData{
    [CustomerNetworking customerWithURL:@"http://echosystem.kibey.com/echofamous/tv/tvs" andParamer:nil andBlock:^(id response) {
        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"tvs"]) {
            CXThirdModel *model = [CXThirdModel modelWithDictionary:dic];
            [self.firstArray addObject:model];
        }
        [self.tableView reloadData];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 130;

    }if (indexPath.row == 1) {
        return 130;

    }
    if (indexPath.row == 2) {
        return 300;

    }
else{
        return 130;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        static NSString *cellID = @"CXFirstTableViewCellID";
        _cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!_cell) {
          _cell = [[CXFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

        }

        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 10)];
        [lable setTextColor:[UIColor grayColor]];
        [lable setText:@"相关频道"];
        [lable setFont:[UIFont systemFontOfSize:12]];
        [_cell addSubview:lable];
        [lable release];
        
    _cell.collectionView.frame = CGRectMake(0, 20, kWidth, 110);
    _cell.collectionView.backgroundColor = [UIColor whiteColor];
    
    [_cell.collectionView setDataSource:self];
    [_cell.collectionView setDelegate:self];
    [_cell.collectionView reloadData];
    [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return _cell;
    }if (indexPath.row == 1)
     {
        static NSString *secondID = @"CXSecondTableViewCellID";
        _secondCell = [tableView dequeueReusableCellWithIdentifier:secondID];
        if (!_secondCell) {
            _secondCell = [[CXSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:secondID];
        }
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 10)];
        [lable setTextColor:[UIColor grayColor]];
        [lable setText:@"好声音入围学院"];
        [lable setFont:[UIFont systemFontOfSize:12]];
        [_secondCell addSubview:lable];
        [lable release];
        
        
        _secondCell.collectionView.frame = CGRectMake(0, 30, kWidth, 100);
        _secondCell.collectionView.backgroundColor = [UIColor whiteColor];
        [_secondCell.collectionView setDataSource:self];
        [_secondCell.collectionView setDelegate:self];
        [_secondCell.collectionView reloadData];
//        _secondCell.collectionView.backgroundColor = [UIColor redColor];
        
        [_secondCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return _secondCell;
    }
    else{
        
        static NSString *cellid = @"CXThirdTableViewCellID";
        _thirdCell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (!_thirdCell) {
            _thirdCell = [[CXThirdTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 10)];
        [lable setTextColor:[UIColor grayColor]];
        [lable setText:@"全部互动节目"];
        [lable setFont:[UIFont systemFontOfSize:12]];
        [_thirdCell addSubview:lable];
        [lable release];
        
        _thirdCell.collectionView.frame = CGRectMake(0, 20, kWidth, 272);
        _thirdCell.collectionView.backgroundColor = [UIColor whiteColor];
        [_thirdCell.collectionView setDataSource:self];
        [_thirdCell.collectionView setDelegate:self];
        [_thirdCell.collectionView reloadData];
        [_thirdCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return _thirdCell;
    }
    
}
#pragma mark collectionView的代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == _cell.collectionView) {
        if (self.thirdArray.count != 0) {
            return  self.thirdArray.count;
        }
        return 0;

    }if (collectionView == _secondCell.collectionView) {
        if (self.secondArray.count != 0) {
            return  self.secondArray.count;
        }
    }
    if (collectionView == _thirdCell.collectionView) {
        if (self.firstArray.count != 0) {
            return self.firstArray.count;
        }
    }
    return 0;
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_cell.collectionView == collectionView) {
        CXFisrtActionColelctionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXFisrtActionColelctionCellID" forIndexPath:indexPath];
        CXInterActionFirstModel *model = [self.thirdArray objectAtIndex:indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
        cell.fisrtModel = model;
        return cell;

    }
    if (_secondCell.collectionView == collectionView) {
        
            CXSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXSecondCollectionViewCellID" forIndexPath:indexPath];
            CXInterSecondModel *model = [self.secondArray objectAtIndex:indexPath.row];
            cell.secondModel = model;
            return cell;

    }
    if (_thirdCell.collectionView == collectionView) {
        CXThirdCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXThirdCollectionViewCellID" forIndexPath:indexPath];
        CXThirdModel *model = [self.firstArray objectAtIndex:indexPath.row];
        cell.thirdModel = model;
        NSLog(@"%@", model.name);
        return cell;

    }
    return nil;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_cell.collectionView == collectionView) {
        NSLog(@"firstCell");
    }else{
        
        NSLog(@"secondCell");
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
